/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.AdminData;
import database.AdminDB;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Muhammad Dony
 */
public class ProfilAdminServlet extends HttpServlet {
    AdminData listProfil;
    AdminDB profildb;
    HttpSession session;
    private String action;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void halamanProfil(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        AdminData admin = (AdminData) session.getAttribute("adminMasuk");;
        request.setAttribute("title", "Profil Admin");
        request.setAttribute("buttonName", "Simpan");
        List listprofil = new AdminDB().getdataAdminById(admin.getID());
        AdminData dataProfil = (AdminData) listprofil.get(0);
        request.setAttribute("dataProfil", dataProfil);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/edit_profil_admin.jsp");
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        session = request.getSession();
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        if (udata != null) {
            try {
                        halamanProfil(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(ProfilAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ProfilAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
        }else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
                rd.forward(request, response);
        }

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    // get session
        session = request.getSession();
        // mengambil object sesiAdmin dari session
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        action = request.getParameter("action");
        if (action.equals("Simpan")) {
                    try {
                        updateProfil(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(ProfilAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ProfilAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    
    public void updateProfil(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listProfil
        listProfil = new AdminData();
        // set listProfil
        listProfil.setID(request.getParameter("ID"));
        listProfil.setUsername(request.getParameter("username"));
        listProfil.setFirstname(request.getParameter("firstname"));
        listProfil.setLastname(request.getParameter("lastname"));
        listProfil.setEmail(request.getParameter("email"));
        listProfil.setPassword(request.getParameter("password"));

        // memasukkannya dalam database
        profildb = new AdminDB();
        profildb.updateProfil(listProfil);
        
        halamanProfil(request, response);
    }
}